// pages/editAddressInfo/editAddressInfo.js
const app = getApp()
import api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLogin: false,
    isShow: true,
    province: '',
    city: '',
    county: '',
    addressInfoItem: {}
  },
  // 选择省
  goToSelectProvince(){
    let obj = {
      p_id:0
    }
    api.POST('/applet/area/findAll', obj, res => {
      console.log(res)
      app.globalData.provinceArr = res.data.data
      wx.navigateTo({
        url: '/pages/province/province'
      })
    })
    
  },

  // 点击按钮保存地址信息
  saveAddressInfo(e){
    console.log(e.detail.value)
    let params = e.detail.value
    if (params.phone.length === 0 || params.name.length === 0 || params.address.length === 0) {
      wx.showToast({
        title: '姓名,手机号码和地址不能为空！',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(params.phone)) {
      wx.showToast({
        title: '请填写正确的手机号码！',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    
    if (params.status) {
      params.status = 2
    } else{
      params.status = 1
    }

    console.log(params.status)
    params.address_id = this.data.addressInfoItem.address_id;
    params.province_name = this.data.province
    params.province_code = app.globalData.province_code
    params.city_name = this.data.city
    params.city_code = app.globalData.city_code
    params.area_name = this.data.county
    params.area_code = app.globalData.county_code
    params.status = params.status

    api.POST('/applet/address/edit', params, res => {
      // console.log(res)
      
      wx.navigateBack({
        delta: 1
      })
    })
  },

  //显示删除 Modal
  showModal() {
    this.setData({
      showLogin: true
    })
  },

  // 取消删除
  handleCancel() {
    this.setData({
      showLogin: false
    })
  },

  // 确认删除
  handleConfirm() {
    let obj = {}
    obj.address_id = this.data.addressInfoItem.address_id
    api.POST('/applet/address/deletes', obj, res => {
      console.log(res)
      wx.navigateBack({
        delta: 1
      })
    })
    this.setData({
      showLogin: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    

    if (app.globalData.selectedProvince){
      // console.log(app.globalData)
      this.setData({
        province: app.globalData.selectedProvince,
        city: app.globalData.selectedCity,
        county: app.globalData.selectedCounty,
        isShow: false
      })
    }
    
    let addressInfoItem = app.globalData.addressInfoItem
    if (addressInfoItem.status === 1) {
      addressInfoItem.status = false
    } else if (addressInfoItem.status === 2) {
      addressInfoItem.status = true
    }
    console.log(addressInfoItem);
    this.setData({
      addressInfoItem
    })
    

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})